package com.sven.consumer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class MainController3 {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/client10")
    public Object client10() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/getHi";

        String respStr = restTemplate.getForObject(url, String.class);
        System.out.println(respStr);

        // ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        // System.out.println("entity：" + entity);
        // System.out.println("entity.getBody(): "  + entity.getBody() );

        return respStr;
    }
    @GetMapping("/client11")
    public Object client11() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/getMap";
        Map<String,String> map = restTemplate.getForObject(url, Map.class);
        System.out.println(map);

        ResponseEntity<Map> entity = restTemplate.getForEntity(url, Map.class);
        System.out.println("entity.getBody(): "  + entity.getBody() );

        return map;
    }

    @GetMapping("/client12")
    public Object client12() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/getObj";
        Person object = restTemplate.getForObject(url, Person.class);
        System.out.println(JSON.toJSONString(object));

        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class);
        System.out.println("entity.getBody(): "  + ToStringBuilder.reflectionToString(entity.getBody()));

        return object;
    }


    @GetMapping("/client13")
    public Object client13() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/getObjParam?name={1}";

        Person object = restTemplate.getForObject(url, Person.class,"maxiaoliu666");
        System.out.println(JSON.toJSONString(object));

        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class,"hehehe...");
        System.out.println("entity.getBody(): "  + JSON.toJSONString(entity.getBody()));

        return object;
    }


    @GetMapping("/client14")
    public Object client14() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/getObjParam?name={name}";

        Map<String, String> map = Collections.singletonMap("name", "xiao66");
        Person object = restTemplate.getForObject(url, Person.class, map);
        System.out.println(JSON.toJSONString(object));

        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class, map);
        System.out.println("entity.getBody(): "  + JSON.toJSONString(entity.getBody()));

        return object;
    }


    @GetMapping("/client15")
    public Object client15(HttpServletResponse response) throws Exception {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/postLocation";

        Map<String, String> map = Collections.singletonMap("name", " memeda");
        URI location = restTemplate.postForLocation(url, map, Person.class);
        response.sendRedirect(location.toURL().toString());
        System.out.println("location："+location);
        System.out.println("location.toURL()："+location.toURL());

        //前边重定向了，这里返回null就行，返回其他的前端也看不见
        return null;
    }

    @GetMapping("/client16")
    public Object client16() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/postParam";

        Map<String, String> map = Collections.singletonMap("name", " memeda");
        Person object = restTemplate.postForObject(url, map, Person.class);
        System.out.println(JSON.toJSONString(object));

        ResponseEntity<Person> entity10 = restTemplate.postForEntity(url, map, Person.class);
        System.out.println("entity10.getBody(): "  + JSON.toJSONString(entity10.getBody()));

        return object;
    }

    @GetMapping("/client17")
    public Object client17() {
        // 自动处理URL
        String url ="http://EUREKA-PROVIDER/postParam";

        // 设置请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置content-type为form-data
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        //使用MultiValueMap封装请求体
        HashMap<String,String> map = new HashMap<>();
        map.put("name","hahahah");
        // 创建http请求实体
        HttpEntity request = new HttpEntity(map, httpHeaders);
        //获取响应实体
        ResponseEntity<Person> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, Person.class);
        System.out.println("entity10.getBody(): "  + JSON.toJSONString(responseEntity.getBody()));

        return responseEntity.getBody();
    }

}

